import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsModule } from './cats/cats.module';
import { CoffeesModule } from './coffees/coffees.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { FilesModule } from './files/files.module';
import { User } from './users/entities/user.entity';
import { Cat } from './cats/entities/cat.entity';
import { Coffee } from './coffees/entities/coffee.entity';
// src/app.module.ts
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import { EventsModule } from './events/events.module';

const logFormat = winston.format.combine(
  winston.format.timestamp(),
  winston.format.json()
);

@Module({
  imports: [
    EventsModule,
    WinstonModule.forRoot({
      format: logFormat,
      transports: [
        new winston.transports.Console({
          level: 'info',
        }),
        new DailyRotateFile({
          level: 'debug',
          dirname: 'logs',
          filename: 'application-%DATE%.log',
          datePattern: 'YYYY-MM-DD',
          zippedArchive: true,
          maxSize: '20m',
          maxFiles: '14d',
        }),
      ],
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get<string>('DB_HOST'),
        port: configService.get<number>('DB_PORT'),
        username: configService.get<string>('DB_USERNAME'),
        password: configService.get<string>('DB_PASSWORD'),
        database: configService.get<string>('DB_DATABASE'),
        entities: [User, Cat, Coffee],
        synchronize: configService.get<boolean>('DB_SYNCHRONIZE'),
        logging: true,
      }),
      inject: [ConfigService],
    }),
    AuthModule,
    CatsModule,
    CoffeesModule,
    FilesModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
